//------------------------------------------------------------------------------
// <pcdd-entitiy>
//     彩票主流业务类库
//     方案中奖业务对象
// </pcdd-entitiy>
//------------------------------------------------------------------------------ 

using CL.Enum.Common;
using CL.Game.DAL;
using CL.Game.Entity;
using CL.Redis.BLL;
using CL.View.Entity.Game;
using System;
using System.Collections.Generic;
using CL.Enum.Common.Lottery;
using CL.Tools.Common;
using CL.Game.BLL.IM;

namespace CL.Game.BLL
{

    /// <summary>
    /// SchemesWinBLL 业务
    /// </summary>
    public class SchemesWinBLL
    {
        SchemesWinDAL dal = new SchemesWinDAL(DbConnectionEnum.CaileGame);

        /// <summary>
        /// 根据方案查询中奖数据
        /// </summary>
        /// <param name="SchemeID"></param>
        /// <returns></returns>
        public List<SchemesWinEntity> QueryEntitysBySchemeID(long SchemeID)
        {
            return dal.QueryEntitysBySchemeID(SchemeID);
        }

        /// <summary>
        /// 系统累计中奖
        /// </summary>
        /// <returns></returns>
        public long QueryTotalWin()
        {
            return dal.QueryTotalWin();
        }
        /// <summary>
        /// 记录中奖信息
        /// </summary>
        /// <param name="ListSql">处理的所有方案号</param>
        /// <param name="ListWinSID">中奖记录方案号</param>
        /// <param name="ListNoWinSID">没有中奖记录方案号</param>
        public void SchemesDetailsWin(List<udv_ComputeTicket> ListSql, List<long> ListWinSID, List<long> ListNoWinSID)
        {
            dal.SchemesDetailsWin(ListSql, ListWinSID, ListNoWinSID);
        }
        /// <summary>
        /// 记录中奖信息
        /// </summary>
        /// <param name="ListSql">处理的所有方案号</param>
        /// <param name="ListWinSID">中奖记录方案号</param>
        /// <param name="ListNoWinSID">没有中奖记录方案号</param>
        public void SchemesDetailsWinByChaseTasks(List<udv_ComputeTicketChaseTasks> ListSql, List<long> ListWinSID, List<long> ListNoWinSID)
        {
            dal.SchemesDetailsWinByChaseTasks(ListSql, ListWinSID, ListNoWinSID);
        }
        /// <summary>
        /// 获取需要派奖的记录
        /// </summary>
        /// <param name="LotteryCode"></param>
        /// <returns></returns>
        public List<udv_SchemesDetailsWin> QueryAwardList(int LotteryCode, string InterfaceType)
        {
            if (InterfaceType == "CaiLe")  //内部算计时 双色球，大乐透等大奖不能自动计算,需要手动派奖
                return dal.QueryFirstPrizeAwardList(LotteryCode);
            else
                return dal.QueryAwardList(LotteryCode);
        }
        /// <summary>
        /// 获取派奖信息(非追号数据)
        /// </summary>
        /// <param name="LotteryCode">彩种</param>
        /// <returns></returns>
        public List<udv_SchemesDetailsWin> QueryAwardListByNotChase(int LotteryCode)
        {
            return dal.QueryAwardListByNotChase(LotteryCode);
        }

        /// <summary>
        /// 获取派奖信息(非追号数据)
        /// </summary>
        /// <param name="LotteryCode">彩种</param>
        /// <returns></returns>
        public List<udv_SchemesDetailsWin> QueryAwardListByChase(int LotteryCode)
        {
            return dal.QueryAwardListByChase(LotteryCode);
        }
        /// <summary>
        /// 报表：中奖查询统计
        /// </summary>
        /// <param name="StartTime">起止时间</param>
        /// <param name="EndTime">岂止时间_止</param>
        /// <param name="UserID">用户ID</param>
        /// <param name="UserName">用户账号</param>
        /// <param name="Mobile">用户手机</param>
        /// <param name="LotteryCode">彩种</param>
        /// <param name="SchemeNumber">订单编号</param>
        /// <param name="PageIndex">当前页</param>
        /// <param name="PageSize">页大小</param>
        /// <param name="RecordAmount">投注总额</param>
        /// <param name="RecordCount">查询总数</param>
        /// <returns></returns>
        public List<udv_WinAwardReport> QueryWinAwardReport(DateTime StartTime, DateTime EndTime, long UserID, string UserName, string Mobile, int LotteryCode, string SchemeNumber, int PageIndex, int PageSize, ref long RecordAmount, ref long RecordWin, ref int RecordCount)
        {
            return dal.QueryWinAwardReport(StartTime, EndTime, UserID, UserName, Mobile, LotteryCode, SchemeNumber, PageIndex, PageSize, ref RecordAmount, ref RecordWin, ref RecordCount);
        }

        #region 自定义方法
        /// <summary>
        /// 系统中奖总额
        /// 局部使用本地缓存 初始化加载提升效率
        /// </summary>
        /// <returns></returns>
        public string SystemTotalWin()
        {
            string CacheKey = "Cache_SystemTotalWin";
            string Rec = CacheHelper.Get<string>(CacheKey);
            if (string.IsNullOrEmpty(Rec))
            {
                string win = (TotalWin() / 100).ToString("N");
                //string[] unitL = { "拾", "佰", "仟", "万", "亿" };
                //if (win.Length > 8) //亿
                //{
                //    Rec = string.Format("{0}{1}{2}{3}元", Convert.ToInt32(win.Substring(0, win.Length - 8)), unitL[4], Convert.ToInt32(win.Substring(win.Length - 8, 4)), unitL[3]);
                //}
                //else if (win.Length > 4 && win.Length <= 8) //万
                //{
                //    Rec = string.Format("{0}{1}{2}元", Convert.ToInt32(win.Substring(0, win.Length - 4)), unitL[3], Convert.ToInt32(win.Substring(win.Length - 4, 4)));
                //}
                Rec = string.Format("{0}元", win);
                //本地缓存
                CacheHelper.Insert(CacheKey, Rec, 10);
            }
            return Rec;
        }
        protected long TotalWin()
        {
            long result = 0;
            result = new SystemRedis().ApplySystemTotalWinRedis();
            if (result == 0)
            {
                var Entity = new SystemSetInfoBLL().QueryEntity("AccumulativeWinMoney");
                long SysVal = Entity == null ? 0 : Convert.ToInt64(Entity.SetValue);
                long WinVal = QueryTotalWin();
                new SystemRedis().ApplySystemTotalWinRedis(SysVal, WinVal);
                result = (SysVal + WinVal);
            }
            return result;
        }
        /// <summary>
        /// 中奖发送推送信息
        /// </summary>
        /// <param name="SchemeID"></param>
        /// <param name="WinMoney"></param>
        /// <returns></returns>
        public bool SendWinMoneyContent(long SchemeID, long WinMoney)
        {
            try
            {
                decimal Amount = WinMoney / 100;
                string Nick = string.Empty;
                long UserCode = 0;
                string Mobie = string.Empty;
                string Token = string.Empty;
                string Content = new TemplateConfigBLL().QueryWinMoneyPushTemplate();
                if (string.IsNullOrEmpty(Content))
                    return false;
                var SchemeEntity = new SchemesBLL().QueryEntity(SchemeID);
                UserCode = SchemeEntity.InitiateUserID;
                var UserEntity = new SystemRedis().SignInByUserCodeRedis(SchemeEntity.InitiateUserID);
                if (UserEntity == null)
                {
                    var User = new UsersBLL().QueryEntityByUserCode(SchemeEntity.InitiateUserID);
                    if (User != null)
                    {
                        Nick = User.UserName;
                        Mobie = User.UserMobile;
                        
                    }
                }
                else
                {
                    Nick = UserEntity.Nick;
                    Mobie = UserEntity.Mobie;
                    Token = UserEntity.Token;
                }
                string TemplateStr = new TemplateConfigBLL().QueryWinMoneyPushTemplate();
                if (string.IsNullOrEmpty(TemplateStr))
                    return false;
                TemplateStr = string.Format(TemplateStr, Nick, Common.GetDescription((LotteryInfo)SchemeEntity.LotteryCode), Amount.ToString("C"));
                #region 发短信
                try
                {
                    if (!string.IsNullOrEmpty(Mobie))
                        SMS.SendModel(Mobie, TemplateStr);
                }
                catch (Exception ex)
                {
                    new Log("Push").Write("中奖短信推送错误：" + ex.Message, true);
                }
                #endregion
                #region 发送跑马灯
                try
                {
                    new Communication().Send_Api_Marquee(5, TemplateStr);
                }
                catch (Exception ex)
                {
                    new Log("Push").Write("中奖发送跑马灯错误：" + ex.Message, true);
                }
                #endregion
                #region 发推送
                try
                {
                    //极光推送
                    //string Msg = string.Format("1,恭喜您 {0},中奖{1} 元", Common.GetDescription((LotteryInfo)SchemeEntity.LotteryCode), Amount.ToString("C"));
                    //new UsersPushBLL().PushMessager_Android_Single(UserCode, "恭喜你中奖", TemplateStr, Msg);
                    if (!string.IsNullOrEmpty(Token))
                    {
                        new Communication().Send_Api_ProAward(Token, Common.GetDescription((LotteryInfo)SchemeEntity.LotteryCode), Amount.ToString("C"));
                    }
                }
                catch (Exception ex)
                {
                    new Log("Push").Write("中奖IM推送错误：" + ex.Message, true);
                }
                #endregion
                #region 房间广播消息
                try
                {
                    TemplateStr = new TemplateConfigBLL().QueryWinAwardNotice();
                    if (string.IsNullOrEmpty(TemplateStr))
                        return false;
                    TemplateStr = string.Format(TemplateStr, Amount.ToString("C"), Common.GetDescription((LotteryInfo)SchemeEntity.LotteryCode), Nick.Length == 1 ? "***" : Nick.Substring(0, 2) + "***");
                    new IM.Communication().Send_Api_Chatrooms_Award(SchemeEntity.RoomCode, TemplateStr);
                }
                catch (Exception ex)
                {
                    new Log("Push").Write("房间广播消息错误：" + ex.Message);
                }
                #endregion

                return true;
            }
            catch (Exception ex)
            {
                new CL.Tools.Common.Log("Push").Write("中奖推送错误：" + ex.Message, true);
                return false;
            }
        }

        #endregion
    }
}
